<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/13 00:50
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Car extends WX_Controller
{
    /**
     * 初始化
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加购物车
     */
    public function add()
    {
        if (!empty($this->openId)) {
            $params = $this->input->post();
            if (empty($params['num']) || empty($params['goodsId']) || $params['num'] < 1 || empty($params['ruleId'])) {
                $this->outputError('参数错误');
            }

            $this->load->model('wx/user/Social_Model', 'social');
            $user = $this->social->getUserByOpenId($this->openId);
            if (empty($user)) {
                $this->outputError('用户不存在');
            }

            $this->load->model('wx/user/Car_Model', 'car');
            if ($this->car->addToCar($user['userid'], $params)) {
                $cacheKey = $this->openId . ':CarCount';
                $this->cache->redis->delete($cacheKey);
                $this->output('添加成功');
            } else {
                $this->outputError('添加失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    public function deleteItem()
    {
        if (!empty($this->openId)) {
            $goodsId = $this->input->post('goodsId');

            $this->load->model('wx/user/Car_Model', 'car');
            $res = $this->car->deleteItem($this->userid, $goodsId);
            if ($res) {
                $cacheKey = $this->openId . ':CarCount';
                $this->cache->redis->delete($cacheKey);
                $this->output(['删除成功']);
            } else {
                $this->outputError('删除失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 修改
     */
    public function modify()
    {
        if (!empty($this->openId)) {

            $goodsId = $this->input->post('goodsId');
            $status = $this->input->post('status');
            $ruleId = $this->input->post('ruleId');

            $this->load->model('wx/user/Car_Model', 'car');
            $res = $this->car->modifySelect($this->userid, $goodsId, $status, $ruleId);
            if ($res) {
                $cacheKey = $this->openId . ':CarCount';
                $this->cache->redis->delete($cacheKey);
                $this->output(['修改成功']);
            } else {
                $this->outputError('修改失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 修改数量
     */
    public function modifyNum()
    {
        if (!empty($this->openId)) {

            $goodsId = $this->input->post('goodsId');
            $num = $this->input->post('num');

            $this->load->model('wx/user/Car_Model', 'car');
            $res = $this->car->modifyNum($this->userid, $goodsId, $num);
            if ($res) {
                $cacheKey = $this->openId . ':CarCount';
                $this->cache->redis->delete($cacheKey);
                $this->output(['修改成功']);
            } else {
                $this->outputError('修改失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 全选/全不选
     */
    public function modifyAll()
    {
        if (!empty($this->openId)) {

            $type = $this->input->post('type');

            $this->load->model('wx/user/Car_Model', 'car');
            $res = $this->car->modifyAll($this->userid, $type);
            if ($res) {
                $this->output(['修改成功']);
            } else {
                $this->outputError('修改失败');
            }
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 商品数量
     */
    public function num()
    {
        if (!empty($this->openId)) {
            $this->load->model('wx/user/Car_Model', 'car');
            $num = $this->car->addToCar($this->userid);
            $this->output(['num' => intval($num)]);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 获取车列表
     */
    public function lists()
    {
        if (!empty($this->openId)) {
            $this->load->model('wx/user/Car_Model', 'car');
            $res = $this->car->getLists($this->userid, $this->userType);
            $this->output(!empty($res) ? $res : []);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

    /**
     * 购物车内瓷商品数量
     */
    public function counter()
    {
        if (!empty($this->openId)) {
            $cacheKey = $this->openId . ':CarCount';
            $res = $this->cache->redis->get($cacheKey);
            if (empty($res)) {
                $this->load->model('wx/user/Car_Model', 'car');
                $counter = $this->car->getCounter($this->userid);
                $this->cache->redis->save($cacheKey, $counter);
            } else {
                $this->load->model('wx/user/Car_Model', 'car');
                $counter = intval($res);
            }

            $this->output(['total' => $counter]);
        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }
}
